<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Model\Category;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;

class CategoryControllers extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $category = new Category();
        $class_tree=$category->tree();
        if($request->has('page')){
            $curr_page=$request->input('page');
        }else {
            $curr_page=1;
        }
        $perPage=5;
        $offset=($curr_page-1)*$perPage;
        $total=count($class_tree);
        $items=array_slice($class_tree, $offset,$perPage);
        $data=new LengthAwarePaginator($class_tree, $total, $perPage,$curr_page,[
            'path'=>Paginator::resolveCurrentPath(),
        ]);
        return view('admin.category.index')->with('data',$data);
    }
    public function changeorder(){
        $input=Input::all();
        $category=Category::find($input['cate_id']);
        $category->cate_order=$input['cate_order'];
        $res=$category->update();
        if($res){
            $data=['status'=>1,
                'msg'=>'修改排序成功'
            ];
        }else {
            $data=['status'=>0,
                'msg'=>'修改排序失败'
            ];
        }
        return $data;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $category=new Category();
        $data=$category->tree();
        return view('admin.category.add')->with('data',$data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $input=Input::except('_token');
        $rules=[
            'cate_name'=>'required',
            'cate_title'=>'required'
        ];
        $message=[
            'cate_name.required'=>'分类名称不能为空',
            'cate_title.required'=>'分类标题不能为空'
        ];
        $validator=Validator::make($input,$rules,$message);
        if($validator->passes()){
            $res=Category::create($input);
            if($res){
                return redirect('admin/category');
            }else {
                return back()->with('msg','添加分类失败');
            }
        }else {
            return back()->withErrors($validator);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $category=new Category();
        $class_tree=$category->tree();
        $cate_info=Category::find($id);
        return view("admin.category.edit")->with('data',$class_tree)->with('cate',$cate_info);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $input=Input::except('_token','_method');
        $res=Category::find($id)->update($input);
        if($res){
            return redirect('admin/category');
        }else {
            return back()->with('msg','修改分类信息失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($cate_id)
    {
        //
        $input=Input::all();
        $class_set=array($input['cate_id']);
        $all_class=Category::orderBy('cate_order')->get();
        $category=new Category();
        $category->get_class_set($input['cate_id'],$class_set,$all_class);
        $flog=true;
        foreach ($class_set as $class){
            $res=Category::find($class)->delete();
            if(!$res){
                $flog=false;
            }
        }
        if($flog){
            $data=['status'=>1,'msg'=>'删除分类信息成功'];
        }else {
            $data=['status'=>0,'msg'=>'删除分类信息失败'];
        }
        return $data;
    }
}
